package org.etom.impl.connector;

import org.eclipse.core.runtime.Plugin;
import org.etom.connector.service.MoodleConnector;
import org.etom.impl.connector.service.MoodleConnectorImpl;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;

/**
 * The activator class controls the plug-in life cycle
 */
public class Activator extends Plugin {

	// The plug-in ID
	public static final String PLUGIN_ID = "EtoM_Connector";

	// The shared instance
	private static Activator plugin;
	
	// The registration reference for the Moodle connector
	private ServiceRegistration sReg;
	
	// The bundle context is stored between the calls of start and stop methods
	private BundleContext context;
	
	/**
	 * The constructor
	 */
	public Activator() {
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.core.runtime.Plugins#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
		this.context = context;
		plugin = this;
		sReg = context.registerService(MoodleConnector.class.getName(),
		    new MoodleConnectorImpl(), null);
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.core.runtime.Plugin#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		plugin = null;
		context = null;
		super.stop(context);
		if (sReg != null) {
		  sReg.unregister();
		}
	}

	/**
	 * The bundle version is specified in the bundle manifest.
	 * The method reads and returns the version.
	 * 
	 * @return - the bundle version as is specified in the manifest
	 */
	public String getBundleVersion() {
	  if (null == context) return null;
	  return context.getBundle().getVersion().toString();
	}
	
	/**
	 * Returns the shared instance
	 *
	 * @return the shared instance
	 */
	public static Activator getDefault() {
		return plugin;
	}

}
